# 多线程

import gym
import numpy as np


# from pymouse import PyMouse


class Env2D(gym.Env):
    def __init__(self):
        # 创建一个10*10的地图,初始值为0
        self.map_ori = np.zeros((10, 10))
        # # 随机生成8个障碍物
        # for i in range(8):
        #     x = np.random.randint(1, 9)
        #     y = np.random.randint(1, 9)
        #     self.map_ori[x][y] = 0.5
        # 将终点设置为0
        self.map_ori[9][9] = 0
        print(self.map_ori)

    def reset(self, *args, **kwargs):
        # 将 0,0 位置设置为1
        self.map = self.map_ori.copy()
        self.map[0][0] = 1
        self.x = 0
        self.y = 0
        return self.map

    def step(self, move):
        # time.sleep(0.3)
        # 将前一个位置设置为0
        self.map[self.x][self.y] = 0
        # 0:上 1:下 2:左 3:右
        if move == 0:
            self.x -= 1
        elif move == 1:
            self.x += 1
        elif move == 2:
            self.y -= 1
        elif move == 3:
            self.y += 1
        # 判断是否出界
        if self.x < 0 or self.x > 9 or self.y < 0 or self.y > 9:
            return self.map, -100, True, {}
        # 判断是否走到障碍物
        if self.map[self.x][self.y] == 0.5:
            return self.map, -100, True, {}
        # 将当前位置设置为1
        self.map[self.x][self.y] = 1
        # 判断是否走到终点
        if self.x == 9 and self.y == 9:
            return self.map, 1, True, {}
        done = False
        if move in [0, 2]:
            reward = -1
        else:
            reward = 1
        print(f'\r########{self.x},{self.y}, {reward}########', end='')
        return self.map, reward, done, {}
